package org.syncsoft.overalt.gui.api.listDataModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.syncsoft.overalt.domain.common.entity.IEntity;

/**
 * 
 * @author tptfc
 *
 */
@SuppressWarnings("serial")
public class AbstractEntityListDataModel<T extends IEntity> extends AbstractListDataModel {
	private List<T> entities;
	
	{
		setEntities(new ArrayList<T>());
	}

	@SuppressWarnings("unchecked")
	@Override
	public <U> U getValue(int index) {
		return (U) (Integer) getCode(index);
	}
	
	public AbstractEntityListDataModel(Collection<T> entities) {
		setEntities(new ArrayList<T>(entities));
		buildList();
	}

	public int getCode(int index) {
		return getEntities().get(index).getCode();
	}
	
	public T getEntity(int index) {
		T entity;
		
		try {
			entity = getEntities().get(index);
		} catch (Exception e) {
			entity = null;
		}
		
		return entity;
	}
	
	
	protected void buildList() {
		for (T entity : getEntities()) {
			addElement(entity.toString());
			
		}
	}
	
	
	//	GETs AND SETs
	protected List<T> getEntities() {
		return entities;
	}

	protected void setEntities(List<T> entities) {
		this.entities = entities;
	}
}
